package com.csxh.web.user.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.csxh.web.user.bean.User;
import com.csxh.web.user.service.UserService;
import com.csxh.web.util.WebUtil;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/user/login.servlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		User u;
		//首先，判断是否已经登录过
        u=WebUtil.getLoginUser(request);
        Boolean remberMe=true;
        String userEmail="";
        String password="";
        boolean useCookieParam=false;
        if(null==u){        	
        	//其次从浏览器cookie中获取登录信息
        	UserService service=new UserService();
        	userEmail=WebUtil.strCookie(request, WebUtil.USER_EMAIL, null);
        	password=WebUtil.strCookie(request, WebUtil.USER_PASSWORD, null);
        	if(null==userEmail){			
        		userEmail=WebUtil.strParam(request, WebUtil.USER_EMAIL, "");
        	}else{
        		useCookieParam=true;
        	}
        	
        	if(null==password){			
        		password=WebUtil.strParam(request, WebUtil.USER_PASSWORD, "");
        	}
        	
        	if(null!=userEmail && null!=password){
        		userEmail=WebUtil.strParam(request, WebUtil.USER_EMAIL, "");
        		u=service.findOneByEmail(userEmail, password);
        		remberMe=WebUtil.boolParam(request, WebUtil.USER_REMBER_ME, false);
        	}
        }
		
		//判断是否登录成功
		String fromURL =WebUtil.getFromUrl(request);
		String path=fromURL;
		if (null==u) {
			log("登录失败");
			//登录不成功
			path=WebUtil.strParam(request, WebUtil.USER_LOGIN_FAIL,fromURL);
		}else{
			//登录成功
			log("登录成功");
			WebUtil.saveLoginUser(request, u);
			path=WebUtil.strParam(request, WebUtil.USER_LOGIN_SUCCESS,fromURL);
			if(remberMe){
				//记住登录信息到cookie中
				Map<String,Object> dataMap=new HashMap<String,Object>();
				dataMap.put(WebUtil.USER_EMAIL, userEmail);
				dataMap.put(WebUtil.USER_PASSWORD, password);
				WebUtil.saveDataToCookie(response,dataMap);
			}
		}
		
		//判断是包含还是转发
		if(null==u){
			if("".equals(userEmail)){
				//说明是验证没有通过，则不要终止请求流程
				log("说明是验证没有通过，则不要终止请求流程");
			}else{
				log("说明是提交表单后，登录不成功，则重定向来要终止请求流程:"+path);
				//说明是提交表单后，登录不成功，则要终止请求流程
				response.sendRedirect(path);
				//request.getRequestDispatcher(path).include(request, response);
				return;
			}
		}else{	
			if("".equals(userEmail)){
				log("说明是验证通过，则要终止请求流程，将请求转发到其它页:"+path);
				//说明是验证通过，则要终止请求流程，将请求转发到其它页
				response.sendRedirect(path);
				//request.getRequestDispatcher(path).forward(request, response);
			}else{
				log("说明是提交表单后，登录成功，则要终止请求流程，重定向到指定页:"+path);
				//说明是提交表单后，登录成功，则要终止请求流程，重定向到指定页
				response.sendRedirect(path);
				return;
			}
			
		}
		
		
	}

}
